<?php
/**
 * 《IP权限》实现类
 *
 * @author 孙晓晔
 * @version $Id: Mac.class.php 841 2010-01-08 04:31:11Z sunxy $
 */
require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class Ip extends EmpUtil {
	public $tbl = Constant::tbl_emp_ip;
	
	public $category = array(
		'-' => '请选择',
		'1' => '上网',		
		'2' => 'QQ',
		
		'5' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '使用中',
		'2' => '未使用',
	
		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'ip' => 'IP',
	
		'createtime' => '创建时间',
		'modifytime' => '更新时间',
	);
	
	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('category_option', $this->category);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/ip/find');
		return true;
	}

	function orderby($px) {
		$ret = " order by id desc";
		if (empty($px) || $px == "-") {
			return $ret;
		} else {
			return ' order by '.$px.' desc';
		}

		return $ret;
	}
	
	function _where($request) {
		$category_s = $request['category_s'];

		$ip_s = $request['ip_s'];
		$out_s = $request['out_s'];
		
		$where = " WHERE 1=1";
		
		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND `category` = '" . $category_s . "'";
		}

		if (!empty($ip_s)) {
			$where .= " AND ip like '" . $ip_s . "%'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);		

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/ip/list');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
				
		$row['category_t'] = $this->category[$row['category']];
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'emp/ip/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $request['id'];
		
		$mac = $request['mac'];

		$ip = $request['ip'];
		$out = $request['out'];

		$content = $request['content'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(emp, mac, ip, `out`, content, createtime, modifytime) VALUES('%s', '%s', '%s'";
		$fmt .= ", '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $emp, $mac, $ip, $out, $content);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->display($request, 'emp/ip/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $request['emp'];

		$ip = $request['ip'];
		
		$content = $request['content'];
		$out = $request['out'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s', ip = '%s', `out` = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $ip, $out, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		if (!empty($request['id'])) {
			$row = $this->_view($request['id']);
		} elseif (!empty($request['ip'])) {
			$row = $this->_view_ip($request['ip']);
		} elseif (!empty($request['mac'])) {
			$row = $this->_view_mac($request['mac']);
		}

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/mac/view');
		return true;
	}
		
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function t($controller, $request) {

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
}
?>